<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>账号管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddUser = true">添加管理员</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="userList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="管理员名称" prop="username"></el-table-column>
        <el-table-column label="失效日期" prop="time"></el-table-column>
        <el-table-column label="操作" width="375px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showPut(scope.row.id)"
            >修改失效日期</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delById(scope.row.id)"
            >删除</el-button>
            <el-button
              type="warning"
              icon="el-icon-s-tools"
              size="mini"
              @click="showputpass(scope.row.id)"
            >修改密码</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 添加管理员 -->
    <el-dialog title="添加管理员" :visible.sync="showAddUser" width="30%" @close="addUserClose">
      <el-form :model="addUserForm" :rules="addUserRules" ref="addUserRef" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addUserForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addUserForm.password"></el-input>
        </el-form-item>
        <el-form-item label="过期时间" prop="time">
          <el-date-picker v-model="addUserForm.time" type="datetime" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddUser = false">取 消</el-button>
        <el-button type="primary" @click="addUser()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改过期时间 -->
    <el-dialog title="修改过期时间" :visible.sync="putDate" width="30%">
      <el-form ref="putFromForm" :model="putFrom" :rules="putFromRules" label-width="80px">
        <el-form-item label="过期时间" prop="time">
          <el-date-picker v-model="putFrom.time" type="datetime" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="putDate = false">取 消</el-button>
        <el-button type="primary" @click="goPut">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改密码 -->
    <el-dialog title="修改密码" :visible.sync="showputDate" width="30%">
      <el-form ref="passForm" :model="passdata" :rules="passRules" label-width="80px">
        <el-form-item label="新密码" prop="password">
          <el-input v-model="passdata.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showputDate = false">取 消</el-button>
        <el-button type="primary" @click="goPutpass">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showAddUser: false,
      putDate: false,
      addUserForm: {},
      addUserRules: {
        username: [{ required: true, message: '请输入登录名称', trigger: 'blur' }],
        password: [{ required: true, message: '请输入登录密码', trigger: 'blur' }],
        time: [{ required: true, message: '请选择活动资源', trigger: 'change' }]
      },
      total: 0,
      pageNum: 1,
      userList: [],
      putFrom: {},
      putFromRules: {
        time: [{ required: true, message: '请选择活动资源', trigger: 'change' }]
      },
      showputDate: false,
      passdata: {
        id: ''
      },
      passRules: {
        password: [{ required: true, message: '请输入新密码', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    addUserClose() {
      this.$refs.addUserRef.resetFields()
    },
    addUser() {
      this.$refs.addUserRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('admin', this.addUserForm)
        if (res.meta.status != 200) {
          return this.$message.error('添加管理员失败！')
        }
        this.$message.success('添加管理员成功！')
        this.showAddUser = false
        this.getUserList()
      })
    },
    async getUserList() {
      const { data: res } = await this.$http.get('admin', {
        params: {
          pageSize: 10,
          pageNum: this.pageNum
        }
      })
      if (res.meta.status != 200) {
        return this.$message.error('获取管理员列表失败！')
      }
      this.userList = res.data.list
      this.total = res.data.total
    },
    handleCurrentChange(newPage) {
      this.pageNum = newPage
      this.getUserList()
    },
    delById(id) {
      this.$confirm('此操作将永久删除该管理员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete('admin', { data: { id: id } })
          if (res.meta.status != 200) {
            return this.$message.error('删除管理员失败！')
          }
          this.$message.success('删除管理员成功！')
          this.getUserList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    showPut(id) {
      this.putDate = true
      this.putFrom.id = id
    },
    async goPut() {
      const { data: res } = await this.$http.put('user', this.putFrom)
      if (res.meta.status != 200) {
        return this.$message.error('修改失效日期失败！')
      }
      this.$message.success('修改失效日期成功！')
      this.putDate = false
      this.getUserList()
    },
    showputpass(id) {
      this.showputDate = true
      this.passdata.id = id
    },
    goPutpass() {
      this.$refs.passForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('password', this.passdata)
        if (res.meta.status != 200) {
          return this.$message.error('修改失败！')
        }
        this.$message.success('修改成功！')
        this.showputDate = false
        this.passdata.password = ''
      })
    }
  }
}
</script>

<style lang="less" scoped>
</style>